<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {},
  data() {
    return {
      appWindow: window,
      form: {
        password: "",
        newPassword: "",
      },

      apiUrl: {
        update: "mall-site-portal/portal-login/updatePassWord",
      },

      boxVisible: false,

      isLoading: false,
    };
  },

  /**  传入的数据
   *  @param {*}  must  是否必须修改   0否  1是
   */

  methods: {
    // 初始化
    async init() {},

    async handleClick() {
      if (!(await this.validate())) {
        return;
      }

      if (this.form.password == this.form.newPassword) {
        this.$vFN.Tools.messageError("新旧密码不能相同");
        return;
      }

      this.isLoading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: {
            ...this.form,
          },
        },
        window
      );

      this.isLoading = false;
      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.boxVisible = false;
        this.onSuccess("success");
      }
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    validateNewPassword(rule, value, callback) {
      if (value !== "") {
        if (!this.$vFN.Utils.isPassword(value)) {
          callback(new Error("密码由8-20位大小写字母和数字的组成"));
        } else if (value == this.form.password) {
          callback(new Error("新旧密码不能相同"));
        } else {
          callback();
        }
      } else {
        callback(new Error("密码不为空"));
      }
    },

    // 成功回调
    onSuccess() {},
    // 取消回调
    onCancel() {},
    // 失败回调
    onError() {},
    // 关闭窗口
    editBoxClose() {
      if (this.must) {
        this.$vFN.Tools.messageError("必须修改");

        return;
      }

      this.boxVisible = false;
      this.onCancel("cancel");
    },
    // 取消
    editBoxCancel() {
      if (this.must) {
        this.$vFN.Tools.messageError("必须修改");
        return;
      }

      this.boxVisible = false;
      this.onCancel("cancel");
    },
  },
  mounted() {
    this.boxVisible = true;
    this.init();
  },
  activated() {},
};
</script>
